﻿using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp10
{
    public class Order
    {
        private int  idnum;
        private Customer customer;
        public OrderDetails orderDetails;
        private int cost = 0;
        public string Customer { get => customer.Name; }
        public int Cost { get => cost; }
        public int Id { get => idnum; set { idnum = value; } }
        public Order()
        {
            idnum = 0;
        }
        public Order (int idnum,Customer c, OrderDetails orderDetail)
        {
            this.idnum = idnum;
            customer = c;
            orderDetails = orderDetail;
            cost = SetCost();
        }
        private int SetCost()
        {
            int c = 0;
            for(int i=0;i< orderDetails.goods.Count;i++)
            {
                c += orderDetails.num[i] * orderDetails.goods[i].Price;
            }
            return c;
        }
        public void Add(Goods g,int n)
        {
            int length = orderDetails.goods.Count;
            for(int i=0;i<length;i++)
            {
                if (orderDetails.Equals(orderDetails.goods[i]))
                    break;
                else if (i == length - 1)
                {
                    orderDetails.Add(g, n);
                }
            }
            cost = SetCost();
        }
        public void EditDetails(string n, int m)
        {
            for (int i = 0; i < orderDetails.goods.Count; i++)
            {
                if (orderDetails.goods[i].Name == n && m == 0)
                {
                    orderDetails.num.Remove(orderDetails.num[i]);
                    orderDetails.goods.Remove(orderDetails.goods[i]);
                }
                else if (orderDetails.goods[i].Name == n)
                    orderDetails.num[i] = m;
                else if (i == orderDetails.goods.Count - 1) throw new Exception("未找到相关商品");
                else continue;
                break;
            }
            cost = SetCost();
        }
        public override bool Equals(object obj)
        {
            Order m = obj as Order;
            return m != null && m.customer == customer && m.orderDetails == orderDetails && m.cost == cost;
        }
        public override string ToString()
        {
            return "订单号：" + idnum + "\n" + customer.ToString() + "\n" + orderDetails.ToString() + "总金额：" + cost + "\n";
        }
        public bool FindGoods(string name)
        {
            var query = from s in orderDetails.goods where name == s.Name select s;
            return query.Count() == 0;
        }
        public void Swap(Order o)
        {
            Order m = new Order();
            m.idnum = this.idnum;
            m.customer = this.customer;
            m.orderDetails = this.orderDetails;
            m.cost = this.cost;
            this.idnum = o.idnum;
            this.customer = o.customer;
            this.orderDetails = o.orderDetails;
            this.cost = o.cost;
            o.idnum = m.idnum;
            o.customer = m.customer;
            o.orderDetails = m.orderDetails;
            o.cost = m.cost;
        }
    }
    public class Customer
    {
        private string name;
        public string Name { get => name; }
        public Customer(string name)
        {
            this.name = name;
        }
        public override string ToString()
        {
            return "下单者：" + name;
        }
    }
}
